<script setup lang="ts">
import { updateUser } from '@/api/system/user';
import { OrderPaymentStatus, OrderPaymentStatusInfo } from '@/api/wuliu';
import { exportWares, queryKhzd, updateWares } from '@/api/wuliu/wares';
import { KhzdData, WaresDocVo } from '@/api/wuliu/wares/types';
import AreaSelector from '@/components/AreaSelector/AreaSelector.vue';
import { Refresh } from '@element-plus/icons-vue';

const loading = ref(false);
const listQuery = reactive<KhzdData>({ deptId: undefined, nickName: undefined });
const listData = ref<KhzdData[]>([]);

async function getCustomData() {
  try {
    loading.value = true;

    const { data } = await queryKhzd(listQuery);
    listData.value = data;
    console.log('ke hu guan li list', data);
  } catch (error) {
    console.error('Error get ke hu guan li list', error);
  } finally {
    loading.value = false;
  }
}
watch(listQuery, getCustomData, { immediate: true });

const requesting = ref(false);
const modifyItem = ref<KhzdData>();

function beforeModifyClose() {
  return !requesting.value;
}

async function handleUpdateItem() {
  const item = toRaw(modifyItem.value);

  if (!item) {
    return;
  }

  try {
    requesting.value = true;
    const { userId, userName, vipPrice } = item;
    await updateUser({ userId, userName, vipPrice });
    console.log('update custom', item);
  } catch (error) {
    console.error('Error update custom', error);
  } finally {
    requesting.value = false;
    handleCloseModifyPaidAmount();
    getCustomData();
  }
}

const modifyPaidAmountVisible = ref(false);

function handleOpenModifyPaidAmount(item: WaresDocVo) {
  modifyItem.value = { ...toRaw(item) };
  modifyPaidAmountVisible.value = true;
}

function handleCloseModifyPaidAmount() {
  modifyPaidAmountVisible.value = false;
}
</script>

<template>
  <div class="app-container ke-hu-guan-li">
    <div class="header">
      <div class="header-filter">
        <el-form ref="queryFormRef" :model="listQuery" :disabled="loading" :inline="true">
          <el-form-item label="所属区域" prop="deptId">
            <AreaSelector v-model="listQuery.deptId" />
          </el-form-item>
          <el-form-item label="客户名称" prop="bdrmc">
            <el-input v-model="listQuery.nickName" />
          </el-form-item>
        </el-form>
      </div>

      <div class="header-actions">
        <el-tooltip content="刷新">
          <el-button :icon="Refresh" circle @click="getCustomData" />
        </el-tooltip>
      </div>
    </div>

    <el-table v-loading="loading" :data="listData" stripe>
      <el-table-column label="客户名称" prop="nickName" />
      <el-table-column label="客户电话" prop="phonenumber" />
      <el-table-column label="客户区域" prop="deptName" />

      <el-table-column label="总件数" prop="wareSum">
        <template #default="{ row }"> {{ row.articleSum }}件 </template>
      </el-table-column>
      <el-table-column label="总运费">
        <template #default="{ row }"> ￥{{ row.priceSum }} </template>
      </el-table-column>
      <el-table-column label="已结算运费">
        <template #default="{ row }"> ￥{{ row.paidAmount }} </template>
      </el-table-column>
      <el-table-column label="客户单价">
        <template #default="{ row }">
          <span>￥{{ row.vipPrice || 0 }}</span>
          <el-button type="primary" size="small" :style="{ marginLeft: '2px' }" text @click="handleOpenModifyPaidAmount(row)">
            <el-icon> <EditPen /></el-icon>
          </el-button>
        </template>
      </el-table-column>
      <!-- <el-table-column label="操作">
        <template #default="{ row }">
          <el-button type="primary" size="small" text @click="handleExportItem(row)">导出</el-button>
        </template>
      </el-table-column> -->
    </el-table>

    <el-dialog v-model="modifyPaidAmountVisible" title="修改客户单价" width="300" :before-close="beforeModifyClose">
      <el-input-number v-model="modifyItem.vipPrice" :min="0" :precision="2" />

      <template #footer>
        <div class="dialog-footer">
          <el-button :disabled="requesting" @click="handleCloseModifyPaidAmount">取消</el-button>
          <el-button :loading="requesting" :disabled="requesting" type="primary" @click="handleUpdateItem">提交</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<style lang="scss">
.ke-hu-guan-li {
  display: flex;
  flex-direction: column;

  .header {
    display: flex;
    align-items: center;
    margin-bottom: 10px;

    .header-filter {
      flex: 1 0 0;
    }

    .header-actions {
      flex: 0 0 auto;
    }
  }
}
</style>
